﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NFine.Code;
using NFine.Data;
using NFine.Domain.Entity.Biz;
using NFine.Domain.Entity.SystemManage;
using NFine.Domain.IRepository.SystemManage;
using NFine.Domain._03_Entity.Biz;
using NFine.Domain._04_IRepository.Biz;
using NFine.Repository.Biz;
using NFine.Repository.SystemManage;

namespace NFine.Application.Biz
{
    public class UserGiveApp
    {
        private IUserGiveRepository userGiveRepository = new UserGiveRepository();
        private IUserRepository userRepository = new UserRepository();
        private IUserPropertyRepository userPropertyRepository = new UserPropertyRepository();
        private UserPropertyApp userPropertyApp = new UserPropertyApp();


        public List<UserGiveEntity> GetList(Pagination pagination, int type, string start, string end, string memberId)
        {

            var expression = ExtLinq.True<UserGiveEntity>();
            DateTime date = DateTime.Now;


            if (!string.IsNullOrEmpty(memberId))
            {
                
                if (type == -1)
                {
                    expression = expression.And(t => t.F_GiveUserId == memberId || t.F_ReceiveUserId == memberId);
                }
                if (type == 0)
                {
                    expression = expression.And(t => t.F_GiveUserId == memberId);
                }
                if (type == 1)
                {
                    expression = expression.And(t => t.F_ReceiveUserId == memberId);
                }
            }


            if (!string.IsNullOrEmpty(start))
            {
                date = Convert.ToDateTime(start);
                expression = expression.And(a => a.F_GiveTime >= date);
            }
            if (!string.IsNullOrEmpty(end))
            {
                date = Convert.ToDateTime(end);
                expression = expression.And(a => a.F_GiveTime <= date);
            }
            List<UserGiveEntity> list = userGiveRepository.FindList(expression, pagination).OrderByDescending(a => a.F_GiveTime).ToList();
            
            return list;

        }


        public int InsertRecord(string giveUserId, string receiveUserId, decimal count, string remarks)
        {
            DateTime curTime = DateTime.Now;

            UserGiveEntity userGiveEntity = new UserGiveEntity();
            userGiveEntity.F_Id = Common.GuId();
            userGiveEntity.F_GiveUserId = giveUserId;
            userGiveEntity.F_ReceiveUserId = receiveUserId;
            userGiveEntity.F_GiveTime = curTime;
            userGiveEntity.F_Remark = remarks;
            userGiveEntity.F_Money = count;
            UserEntity userGive = userRepository.FindEntity(giveUserId);
            UserEntity userReceive = userRepository.FindEntity(receiveUserId);
            userGiveEntity.F_GiveUserName = userGive.F_RealName;
            userGiveEntity.F_ReceiveUserName = userReceive.F_RealName;
            userGiveEntity.F_SerialNumber = Common.CreateNo();

            UserPropertyEntity givePropertyEntity = userPropertyApp.GetMemberProperty(giveUserId);
            UserPropertyEntity receivePropertyEntity = userPropertyApp.GetMemberProperty(receiveUserId);

            if (givePropertyEntity == null)
                return 0;

            if (receivePropertyEntity == null)
                return 0;

            givePropertyEntity.F_Property -= count;
            givePropertyEntity.F_LastModifyTime = curTime;
            receivePropertyEntity.F_Property += count;
            receivePropertyEntity.F_LastModifyTime = curTime;
            try
            {
                //开启事务
                using (var db = new RepositoryBase().BeginTrans())
                {
                    db.Insert(userGiveEntity);
                    db.Update(givePropertyEntity);
                    db.Update(receivePropertyEntity);

                    return db.Commit();
                }
            }
            catch
            {
                return 0;
            }

        }


    }
}
